﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using System.Web.Security;
using ZLCMS.Business.Commands.SessionCommands;
using DotNet.Common.Command;
using ZLCMS.Business.Util;
using DotNet.Common.NHibernateUtil;
using NHibernate;
using ZLCMS.Business.Shop.Commands.Member;

namespace ZLCMS.Web.MemberUser
{
    public partial class SendMessage : System.Web.UI.Page
    {
  protected MemberInfo _memberDb;

        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void btnSendEmail_Click(object sender, EventArgs e)
        {
            ClientScriptManager csm = this.Page.ClientScript;
              using (ISession session = SessionFactoryHelper.CreateSession())
                {
                    MemberInfo memberDb = session.CreateQuery(" from MemberInfo where Email=:email ").SetParameter("email", txt_account.Text.Trim()).UniqueResult<MemberInfo>();
                  if(memberDb==null)
                  {
                      csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('邮箱不存在');", true);
                  }
            //发送激活邮件
            MailKit mail = new MailKit();
            EmailCheckInfo _checkinfo = new EmailCheckInfo();
            _checkinfo.MemberAccount = memberDb.AccountId;
            _checkinfo.CheckString = FormsAuthentication.HashPasswordForStoringInConfigFile(memberDb.AccountId , "MD5").ToLower();
            _checkinfo.OptTime = DateTime.Now;
            _checkinfo.Status = "0";
            session.Save(_checkinfo);
            string _content = "<a href='" + ServerUtil.GetRootURI()+ "/MemberUser/ResetPassword.aspx?type=emailresetpassword&checkstring=" + _checkinfo.CheckString + "&uid=" + _checkinfo.MemberAccount + "'>重置密码</a>";

            mail.SendMailAndWriteLog(memberDb.Email, "重置密码", _content, memberDb.Email);
              }
        }

        protected void btnSendCheckCode_Click(object sender, EventArgs e)
        {
            ClientScriptManager csm = this.Page.ClientScript;
            //发送手机验证码
            string phone = txt_account.Text.Trim(); 
       
            SendCheckCodeMsgResult cmdrs = (SendCheckCodeMsgResult)new SendCheckCodeMsgCommand().Execute(new SendCheckCodeMsgParameter()
            {
                Telephone = phone,
                OptType = "CR"
            }).Result;
            if (cmdrs.result == 2)
            {
                csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('操作成功。');window.location.href='/MemberUser/resetpassword.aspx?type=phoneresetpassword&uid=" + phone + "'", true);
            }
            else
            {
                csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('发送失败。');", true);
            }

        }

        protected void SaveUser()
        {
            ClientScriptManager csm = this.Page.ClientScript;

         


            csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('操作成功。');window.location.href='/MemberUser/Login.aspx'", true);
        }
    }
}
